package org.ljdp.bootstrap.startup;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ThreadStartup {
	private String bootThread;
	private String shutdownThread;
	
	public ThreadStartup(String bootThread, String shutdownThread) {
		super();
		this.bootThread = bootThread;
		this.shutdownThread = shutdownThread;
	}
	
	public void run(String appname) {
		System.out.println("【"+appname+"】开始启动");
		
		try {
			if(shutdownThread != null) {
				Class shutdownCls = Class.forName(shutdownThread);
				Thread shutdownThread = (Thread)shutdownCls.newInstance();
				Runtime.getRuntime().addShutdownHook(shutdownThread);
			}
			
			//启动业务线程
			System.out.println("开始启动业务线程..");
			Class bootCls = Class.forName(bootThread);
			Runnable bootRun = (Runnable)bootCls.newInstance();
			new Thread(bootRun).start();
			
			System.out.println("--------------------[命令说明]-----------------------------");
			System.out.println("shutdown：关闭应用");
			System.out.println("-----------------------------------------------------------");
			BufferedReader cmd = new BufferedReader(new InputStreamReader(System.in));
			try {
				String cmdKey;
				while (!(cmdKey = cmd.readLine()).equalsIgnoreCase("shutdown")) {
					System.out.println("your input : " + cmdKey);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				System.out.println("【"+appname+"】正在退出");
				System.exit(0);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
